﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Set the saturation of an image
    /// </summary>
    public class Saturation : BaseImageFilter
    {
        protected float m_fSaturation = 0.0f;
        
        /// <summary>
        /// Default constructor
        /// </summary>
        public Saturation()
        {
        }

        /// <summary>
        /// primary constructor
        /// specify saturation directly
        /// </summary>
        /// <param name="_fSaturation">saturation amount</param>
        public Saturation(float _fSaturation)
        {
            m_fSaturation = _fSaturation;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_saturation"></param>
        internal Saturation(Saturation _saturation)
        {
            this.m_fSaturation = _saturation.m_fSaturation;
        }

        /// <summary>
        /// Gets or sets the saturation amount
        /// </summary>
        public float Intensity
        {
            get { return m_fSaturation; }
            set { m_fSaturation = value; }
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            Color color = Color.Transparent;

            int nWidth = _bitmap.Width;
            int nHeight = _bitmap.Height;

            int y = 0;
            int x = 0;
            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    color = _bitmap.GetPixel(x, y);
                    HSB hsb = new HSB(color);
                    hsb.S = m_fSaturation;
                    _bitmap.SetPixel(x, y, hsb.ToColor());
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Saturation(this);
        }
    }
}
